package com.scholarship.system.controller;

import com.scholarship.system.common.dto.Result;
import com.scholarship.system.service.ResearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.MonthDay;
import java.time.Year;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 科研能力评分控制器
 */
@RestController
@RequestMapping("/score/research")
public class ResearchController {

    @Autowired
    private ResearchService researchService;

    /**
     * 获取当前学期
     */
    @GetMapping("/current-term")
    public Result<Map<String, Object>> getCurrentTerm() {
        Map<String, Object> result = new HashMap<>();
        // 获取当前年份
        int year = Year.now().getValue();
        // 根据月份判断学期
        int month = MonthDay.now().getMonthValue();
        String semester = month >= 2 && month <= 7 ? "春季" : "秋季";

        result.put("year", year);
        result.put("semester", semester);

        return Result.success(result);
    }

    /**
     * 获取科研评分列表
     */
    @GetMapping("/list")
    public Result<List<Map<String, Object>>> getResearchScoreList(
            @RequestParam(value = "advisorId", required = false) Long advisorId,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "semester", required = false) String semester) {
        return researchService.getResearchScoreList(advisorId, keyword, year, semester);
    }

    /**
     * 获取科研评分详情
     */
    @GetMapping("/detail")
    public Result<Map<String, Object>> getResearchScoreDetail(
            @RequestParam("studentId") Long studentId,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "semester", required = false) String semester) {
        // 设置默认值
        if (year == null) {
            year = Year.now().getValue();
        }
        if (semester == null) {
            int month = MonthDay.now().getMonthValue();
            semester = month >= 2 && month <= 7 ? "春季" : "秋季";
        }

        return researchService.getResearchScoreDetail(studentId, year, semester);
    }

    /**
     * 保存科研评分
     */
    @PostMapping("/save")
    public Result<Void> saveResearchScore(@RequestBody Map<String, Object> params) {
        return researchService.saveResearchScore(params);
    }
}